import { Component, OnInit, Input, OnDestroy, OnChanges, SimpleChange } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { TreeNode, DialogService } from 'primeng/primeng';
import { FreeflowService } from '../freeflow.service';
import { FfAddBizProcComponent } from '../bizproc/ff-addbizproc.component';
import { Subscription } from 'rxjs';

@Component({
  selector: 'ff-builder',
  templateUrl: './ffbase.component.html',
  styleUrls: ['./ffbase.component.scss'],
  providers: []
})
export class FfBaseComponent implements OnInit, OnChanges {
  currentBiz: number = 0;
  @Input()
  set bizId(bizId: number) {
    if (!bizId) return;
    this.currentBiz = bizId;
    this.initBizProcTree("");
  }
  get bizId(): number { return this.currentBiz; }

  @Input()
  procDefId: number = 0;

  loading: boolean = true;
  selectedNode: TreeNode;
  procTree: TreeNode[];
  subscription: Subscription;

  constructor(
    private router: Router,
    private dialogService: DialogService,
    private FreeflowService: FreeflowService) {
    this.subscription = FreeflowService.routeMsgPush$.subscribe(
      data => {
        this.initBizProcTree(data);
      });
  }

  ngOnInit() {
    this.initBizProcTree("");
  }
  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
  /** 初始化业务流程树  */
  initBizProcTree(defauleCheckedNode: string) {
    this.FreeflowService.getProceDefTree(this.bizId, this.procDefId).subscribe(data => {
      this.loading = false;
      this.procTree = data;
      if ("" == defauleCheckedNode || null == defauleCheckedNode) {
        /** 默认选中第一个节点 */
        if (data[0] && data[0].children && data[0].children.length > 0) {
          this.selectedNode = data[0].children[0]
        } else {
          this.selectedNode = data[0]
          this.procDefId = this.selectedNode['id'];
        }
      } else {
        this.checkNodeRecursive(this.procTree, defauleCheckedNode, 1);
      }

      this.nodeSelect(null);
    },
      error => console.error(error)
    );
  }

  /**节点选择事件 */
  nodeSelect(event) {
    this.procDefId = this.selectedNode['id'];
  }

  /** 创建业务类型 */
  addBizProc() {
    const ref = this.dialogService.open(FfAddBizProcComponent, {
      'header': '新增业务过程',
      'width': '400px',
      contentStyle: { "height": "240px" }
    });
    ref.onClose.subscribe((data: any) => {
      if (data) {
        this.loading = true;
        this.initBizProcTree(data);
      }
    });
  }

  checkNodeRecursive(nodes: TreeNode[], nodeId: string, level: number) {
    nodes.forEach(node => {
      if ('biz_' + node['id'] == nodeId && level == 1) {
        this.selectedNode = node;
        return;
      }
      if ('proc_' + node['id'] == nodeId && level == 2) {
        this.selectedNode = node;
        return;
      }
      if (node.children) {
        this.checkNodeRecursive(node.children, nodeId, 2);
      }
    });
  }

  ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
    for (let propName in changes) {
      if (propName == 'procDefId') {
        this.initBizProcTree('');
      }
    }
  }
}
